@echo off
setlocal enabledelayedexpansion
chcp 65001 >nul
title EXIF Viewer Build Tool

echo.
echo ==========================================
echo          EXIF Viewer Build Tool  
echo ==========================================
echo.

:main_menu
echo [1] Full Build (Recommended)
echo [2] Quick Build
echo [3] Offline Build
echo [4] Run Program
echo [5] Git Init
echo [6] Clean Build
echo [7] Help
echo [8] Exit
echo.

set /p choice=Select option (1-8): 

if "%choice%"=="1" goto full_build
if "%choice%"=="2" goto quick_build
if "%choice%"=="3" goto offline_build
if "%choice%"=="4" goto run_program
if "%choice%"=="5" goto git_init
if "%choice%"=="6" goto clean_build
if "%choice%"=="7" goto show_help
if "%choice%"=="8" goto exit
goto invalid_choice

:full_build
echo.
echo ========== Full Build Mode ==========
echo Creating virtual environment...
if not exist .venv (
    python -m venv .venv
    if errorlevel 1 (
        echo Error: Failed to create virtual environment
        pause
        goto main_menu
    )
)

echo Activating virtual environment...
call .venv\Scripts\activate.bat

echo Upgrading pip...
python -m pip install --upgrade pip

echo Installing dependencies...
pip install -r requirements.txt
pip install pyinstaller

goto do_build

:quick_build
echo.
echo ========== Quick Build Mode ==========
if exist .venv\Scripts\activate.bat call .venv\Scripts\activate.bat
pip install pyinstaller --quiet
goto do_build

:offline_build
echo.
echo ========== Offline Build Mode ==========
echo Note: This mode assumes PyInstaller is already installed
if exist .venv\Scripts\activate.bat call .venv\Scripts\activate.bat
goto do_build

:do_build
echo.
echo Cleaning old build files...
if exist dist rmdir /s /q dist
if exist build rmdir /s /q build

echo Building exe file...
python -m PyInstaller --onefile --windowed --name="EXIF_Viewer" --hidden-import=PIL --hidden-import=PIL.Image --hidden-import=PIL.ImageTk --hidden-import=PIL.ImageDraw --hidden-import=PIL.ImageFont --hidden-import=exifread --hidden-import=tkinter --hidden-import=tkinter.filedialog --hidden-import=tkinter.ttk --hidden-import=tkinter.scrolledtext exif_viewer_gui.py

if errorlevel 1 (
    echo Build failed!
    pause
    goto main_menu
)

echo.
echo ========== Build Successful! ==========
if exist "dist\EXIF_Viewer.exe" (
    echo Generated file: dist\EXIF_Viewer.exe
    dir dist\EXIF_Viewer.exe | findstr "EXIF_Viewer.exe"
    echo.
    set /p test_run=Test run? (Y/N): 
    if /i "!test_run!"=="Y" start "" "dist\EXIF_Viewer.exe"
)
pause
goto main_menu

:run_program
echo.
if exist "dist\EXIF_Viewer.exe" (
    echo Starting EXIF Viewer...
    start "" "dist\EXIF_Viewer.exe"
) else if exist "exif_viewer_gui.py" (
    echo Running Python version...
    if exist .venv\Scripts\activate.bat call .venv\Scripts\activate.bat
    python exif_viewer_gui.py
) else (
    echo Error: No executable found, please build first
    pause
)
goto main_menu

:git_init
echo.
echo ========== Git Repository Initialization ==========
if exist ".git" (
    echo This directory is already a Git repository
    git status --short
) else (
    echo Initializing Git repository...
    git init
    if errorlevel 1 (
        echo Error: Git initialization failed, make sure Git is installed
        pause
        goto main_menu
    )
    
    echo Adding files...
    git add .
    
    set /p commit_msg=Enter commit message (press Enter for default): 
    if "!commit_msg!"=="" set commit_msg=Initial commit: EXIF Viewer project
    
    git commit -m "!commit_msg!"
    echo Git repository initialization complete!
)
pause
goto main_menu

:clean_build
echo.
echo Cleaning build files...
if exist dist (
    rmdir /s /q dist
    echo Deleted dist directory
)
if exist build (
    rmdir /s /q build
    echo Deleted build directory
)
if exist *.spec (
    del /q *.spec
    echo Deleted spec files
)
echo Cleanup complete!
pause
goto main_menu

:show_help
echo.
echo ==========================================
echo                Help Information
echo ==========================================
echo.
echo Functions:
echo [1] Full Build - Create virtual environment, install all dependencies
echo [2] Quick Build - Skip environment setup, build directly
echo [3] Offline Build - Build without network, requires pre-installed PyInstaller
echo [4] Run Program - Run built exe or Python version
echo [5] Git Init - Initialize Git repository and create first commit
echo [6] Clean Build - Delete all build-generated files
echo.
echo System Requirements:
echo - Windows 7+ operating system
echo - Python 3.7+ (for building)
echo - About 100MB disk space (build process)
echo.
echo Generated Files:
echo - dist\EXIF_Viewer.exe (about 30-60MB)
echo.
echo Project Features:
echo - View image EXIF information
echo - Image preview
echo - Smart watermark addition
echo - Multi-format support (JPG/PNG/GIF/BMP/TIFF)
echo.
pause
goto main_menu

:invalid_choice
echo.
echo Invalid selection, please try again
pause
goto main_menu

:exit
echo.
echo Exiting build tool
exit /b 0